package com.dkts.oil.converter;

import com.dkts.oil.config.OilConfig;
import com.dkts.oil.enumes.WriterTypeEnums;
import com.dkts.oil.mysql.entity.OilObstacle;
import com.dkts.oil.strtree.GeoJsonSTRtreeIndex;
import com.dkts.oil.web.vo.request.OilStorageTankRequest;
import com.dkts.oil.web.vo.request.subReq.ObstacleType;
import com.dkts.oil.web.vo.request.subReq.SqlWhere;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.locationtech.jts.geom.Geometry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Schema(description = "油库选址转换类")
@Slf4j
@Service
public class OilAddressConverter {
    @Autowired
    private  GeoJsonSTRtreeIndex geoJsonSTRtreeIndex;
    public List<Geometry> listObstacles(Geometry planGeometry2, OilStorageTankRequest request, String url) throws Exception {
        List<OilObstacle> data= geoJsonSTRtreeIndex.queryByPolygon(planGeometry2,GeoJsonSTRtreeIndex.strTreeBuild);
        List<Geometry> listObstacles=new ArrayList<>();
        if(data!=null) {
            log.info("障碍物数据：{}", data.size());
            for (OilObstacle oilObstacle : data) {
                Geometry geometry = GeometryConverter.convert(oilObstacle.getGeoData());
                WriterToFile.writerFileGeoJson(url+"约束条件All-"+".geojson",geometry);
                //添加规则约束条件
                List<SqlWhere> rules=request.getRules();
                if(rules != null){
                    for (SqlWhere sqlWhere : rules) {

                        if(sqlWhere.getBufferWidth() == null){
                            throw new Exception("障碍物缓冲半径不能为空");
                        }

                        //所有添加缓冲区域
                        if("ALL".equals(sqlWhere.getType())){
                            Geometry de=GeometryConverter.getBuff(geometry,sqlWhere.getBufferWidth());
                            Geometry geometry1De= GeometryConverter.difference(de,geometry);
                            listObstacles.add(geometry1De);
                            WriterToFile.writerFileGeoJson(url+"-ALL.geojson",geometry1De);
                            continue;
                        }

                        Integer label= oilObstacle.getLabel();
                        //代表所有的水域
                        if("label".equals(sqlWhere.getType()) && sqlWhere.getValue() == 4){

                            //label >=40 and <=49  or label=-103  代表是水域
                            if(label >=40 && label <=49 || label == -103){
                                Geometry de=GeometryConverter.getBuff(geometry,sqlWhere.getBufferWidth());
                                Geometry geometry1De= GeometryConverter.difference(de,geometry);
                                listObstacles.add(geometry1De);
                                WriterToFile.writerFileGeoJson(url+"-水.geojson",geometry1De);
                            }
                            continue;
                        }

                        //城乡、工矿、居民用地
                        if("label".equals(sqlWhere.getType()) && sqlWhere.getValue() == 5){
                            if(label >=50 && label <=59 || label == -100){
                                Geometry de=GeometryConverter.getBuff(geometry,sqlWhere.getBufferWidth());
                                Geometry geometry1De= GeometryConverter.difference(de,geometry);
                                listObstacles.add(geometry1De);
                                WriterToFile.writerFileGeoJson(url+"-土地.geojson",geometry1De);
                            }
                            continue;
                        }
                        //指定固定区域 直接过滤
                        if("label".equals(sqlWhere.getType()) && WriterTypeEnums.check(sqlWhere.getValue()) && sqlWhere.getValue() == label){
                            Geometry de=GeometryConverter.getBuff(geometry,sqlWhere.getBufferWidth());
                            Geometry geometry1De= GeometryConverter.difference(de,geometry);
                            listObstacles.add(geometry1De);
                            WriterToFile.writerFileGeoJson(url+"-固定区域"+label+".geojson",geometry1De);
                            continue;
                        }
                    }
                }
                listObstacles.add(geometry);
            }
        }
        if(request.getObstacle()!=null){
            log.info("手动设置障碍物：{}", request.getObstacle().size());
            List<ObstacleType> d=request.getObstacle();
            for (ObstacleType obstacleType : d) {
                Geometry geometry = GeometryConverter.convert(obstacleType.getBoundWkt());
                WriterToFile.writerFileGeoJson(url+"-手动绘制.geojson",geometry);
                if(obstacleType.getBufferWidth() != null && obstacleType.getBufferWidth() > 0){
                    Geometry geometry2= GeometryConverter.getBuff(geometry, obstacleType.getBufferWidth());
                    WriterToFile.writerFileGeoJson(url+"-手动绘制---缓冲区.geojson",geometry2);
                    listObstacles.add(geometry2);
                    continue;
                }
                listObstacles.add(geometry);
            }
        }
        return listObstacles;
    }
}
